package com.yuanye.gwcit.fragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;

import androidx.annotation.Nullable;

import com.yuanye.gwcit.GW;
import com.yuanye.yeecho.base.CommonTaskFragment;
import com.yuanye.yeecho.utils.WifiUtil;
import com.yuanye.yeecho.utils.YY;

import java.util.HashMap;
import java.util.List;

public class WifiFragment extends CommonTaskFragment {

    WifiManager wifiManager;
    WifiBroadcastReceiver wifiReceiver;

    public WifiFragment() {
        setTaskName("WIFI测试");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        wifiManager = WifiUtil.getInstance(context).getWifiManager();
    }

    @Override
    public void execTask() {
        super.execTask();
        String wifiModel = YY.execShell("getprop persist.vendor.wifi_name");
        String stdWifiModel = GW.cfgPcba.getWifiModel();
        appendLog("WIFI型号："+wifiModel);
        if (wifiModel.equals(stdWifiModel)){
            appendLog("型号比对成功\n");
        }else{
            appendLogF("型号比对失败，参考值："+stdWifiModel);
            setFail();
            return;
        }
        runInBackground();
        if (wifiManager != null) {
//            runInBackground();
            IntentFilter filter = new IntentFilter();
            filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
//                filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
            wifiReceiver = new WifiBroadcastReceiver();
            context.registerReceiver(wifiReceiver, filter);
            appendLog("正在搜索wifi..");
            wifiManager.startScan();

        }else{
            appendLogF("未找到wifi服务");
            setFail();
        }
    }

    @Override
    public void stopTask() {
        super.stopTask();
        if (wifiReceiver != null){
            context.unregisterReceiver(wifiReceiver);
        }
    }

    class WifiBroadcastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            List<ScanResult> scanResults = wifiManager.getScanResults();
            HashMap<String, Integer> map = new HashMap<>();
            for (ScanResult result : scanResults){
                if (!map.containsKey(result.SSID)){
                    map.put(result.SSID, result.level);
                    appendLog("WIFI名称：" + result.SSID + "  ---  WIFI信号：" + result.level + "dBm");
                }else{
//                    callback.info("WIFI名称：" + result.SSID + "  ---  WIFI信号：" + result.level + "dBm -- 被忽略的 -- ");
                }

            }
//            int size = scanResults.size();
            int size = map.size();
            appendLog("搜索结束，发现wifi个数："+size);
            if (size > 0){
//                setPass();
                passAndNext();
            }else{
                appendLogF("未发现wifi");
                setFail();
            }
        }
    }

}
